﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SampleFramework;

namespace starpillage.Input
{
    public class Keyboard
    {
        Dictionary<Keys, Action<GameTime>> registeredKeyActions = new Dictionary<Keys, Action<GameTime>>();
        List<Action<GameTime>> actionsForNextUpdate = new List<Action<GameTime>>();

        public void SetKeyEventHandlers(Control control)
        {
            control.KeyDown += _KeyDown;
            control.KeyUp += _KeyUp;
        }

        public void RegisterKeyAction(Keys key, Action<GameTime> action)
        {
            registeredKeyActions[key] = action;
        }

        public void Update(GameTime gameTime)
        {
            foreach (var action in actionsForNextUpdate)
            {
                action(gameTime);
            }
            actionsForNextUpdate.Clear();
        }

        void _KeyUp(object sender, KeyEventArgs e)
        {
        }

        void _KeyDown(object sender, KeyEventArgs e)
        {
            Action<GameTime> registeredAction;
            if(registeredKeyActions.TryGetValue(e.KeyCode, out registeredAction))
            {
                actionsForNextUpdate.Add(registeredAction);
            }
        }
    }
}
